package cn.bjit.controller;
import cn.bjit.controller.request.LinkGroupAddRequest;
import cn.bjit.controller.request.LinkGroupUpdateRequest;
import cn.bjit.enums.BizCodeEnum;
import cn.bjit.service.LinkGroupService;
import cn.bjit.util.JsonData;
import cn.bjit.vo.LinkGroupVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/group/v1/")
public class LinkGroupController {
    @Resource
    private LinkGroupService linkGroupService;
    /**
     * 添加组
     * @return
     */
    @PostMapping("/add")
   public JsonData add(@RequestBody LinkGroupAddRequest linkGroupAddRequestRequest){
      int row=  linkGroupService.add(linkGroupAddRequestRequest);
        return row==1?JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.GROUP_ADD_FAIL);
   }

    /**
     * 根据id删除组
     * @param groupId
     * @return
     */
    @DeleteMapping("/del/{group_id}")
   public JsonData del(@PathVariable("group_id") Long groupId){
         int row=linkGroupService.del(groupId);
         return row==1?JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.GROUP_NOT_EXIST);
   }

    /**
     * 根据id查找分组详情
     * @param groupId
     * @return
     */
  @GetMapping("/detail/{group_id}")
   public JsonData detail(@PathVariable("group_id") Long groupId){
        LinkGroupVO linkGroupVO=linkGroupService.detail(groupId);
        return JsonData.buildSuccess(linkGroupVO);
   }
    /**
     * 查询用户全部的分组
     * @return
     */
    @GetMapping("/list")
   public JsonData findUserAllLinkGroup(){
      List<LinkGroupVO> list=linkGroupService.listAllGroup();
      return JsonData.buildSuccess(list);
   }
    /**
     * 修改用户分组
     * @return
     */
    @PutMapping("/update")
   public JsonData update(@RequestBody LinkGroupUpdateRequest linkGroupUpdateRequest){
      int row= linkGroupService.updateById(linkGroupUpdateRequest);
      return row==1?JsonData.buildSuccess():JsonData.buildResult(BizCodeEnum.GROUP_OPER_FAIL);
   }
}

